//
//  HHTabBarController.m
//  HaoHint
//
//  Created by winter on 16/5/22.
//  Copyright © 2016年 wd. All rights reserved.
//

#import "HHTabBarController.h"
#import "HaoHintViewController.h"
#import "CountdownViewController.h"
#import "FindFunViewController.h"


static NSString *const HHTabBarItemTitle = @"tabBarItemTitle";
static NSString *const HHTabBarItemImage =  @"tabBarItemImage";
static NSString *const HHTabBarItemSelectedImage =  @"tabBarItemSelectedImage";

@interface HHTabBarController ()
@property (nonatomic, strong) NSArray *tabBarItemsAttrobutes;
@end

@implementation HHTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setUpViewControllers];
}

#pragma mark - setUI method

- (void)setUpViewControllers
{
    HaoHintViewController *haoHintVC = [[HaoHintViewController alloc] initWithNibName:@"HaoHintViewController" bundle:nil];
    UINavigationController *navc1 = [[UINavigationController alloc] initWithRootViewController:haoHintVC];
    
    CountdownViewController *countdownVC = [[CountdownViewController alloc] initWithNibName:@"CountdownViewController" bundle:nil];
    UINavigationController *navc2 = [[UINavigationController alloc] initWithRootViewController:countdownVC];
    
    FindFunViewController *finFunVC = [[FindFunViewController alloc] initWithNibName:@"FindFunViewController" bundle:nil];
    UINavigationController *navc3 = [[UINavigationController alloc] initWithRootViewController:finFunVC];
    
    [self customizeTabBarForController];
    [self setViewControllers:@[
                               navc2,
                               navc1,
                               navc3
                               ]];
}

- (void)customizeTabBarForController
{
    NSDictionary *dic1 = @{
                           HHTabBarItemTitle : @"好提醒",
                           HHTabBarItemImage : @"tabbar_icon",
                           HHTabBarItemSelectedImage : @"tabbar_icon"
                           };
    NSDictionary *dic2 = @{
                           HHTabBarItemTitle : @"倒计时",
                           HHTabBarItemImage : @"tabbar_icon",
                           HHTabBarItemSelectedImage : @"tabbar_icon"
                           };
    NSDictionary *dic3 = @{
                           HHTabBarItemTitle : @"好玩",
                           HHTabBarItemImage : @"tabbar_icon",
                           HHTabBarItemSelectedImage : @"tabbar_icon"
                           };
    NSArray *tabBarItemAttributes = @[
                                      dic2,
                                      dic1,
                                      dic3];
    self.tabBarItemsAttrobutes = tabBarItemAttributes;
}

- (void)setViewControllers:(NSArray *)viewControllers
{
    if (viewControllers && [viewControllers isKindOfClass:[NSArray class]]) {
        NSUInteger idx = 0;
        for (UIViewController *viewController in viewControllers) {
            [self addOneChildViewController:viewController
                                  withTitle:_tabBarItemsAttrobutes[idx][HHTabBarItemTitle]
                            normalImageName:_tabBarItemsAttrobutes[idx][HHTabBarItemImage]
                          selectedImageName:_tabBarItemsAttrobutes[idx][HHTabBarItemSelectedImage]];
            idx++;
        }
    }
}

- (void)addOneChildViewController:(UIViewController *)viewController
                        withTitle:(NSString *)title
                  normalImageName:(NSString *)normalImageName
                selectedImageName:(NSString *)selectedImageName
{
    viewController.tabBarItem.title = title;
    
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    normalImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.image = normalImage;
    
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = selectedImage;
    
    viewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -15);
    
    [self addChildViewController:viewController];
}

@end
